#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define M 512
#define L 32
#define INF 0x3f3f3f3f
struct {
    char name[30];
    int change;
} StationID_list[M];
struct {
    int wgt, id;
} map[M][M];
int id, each_staNum, lineNum, IsChange, all_staNum, pre[M], dis[M], path[M], top;
char st[L], ed[L];

int exist(char *name) {
    for (int i = 0; i < all_staNum; i++)
        if (strcmp(name, StationID_list[i].name) == 0)
            return i;
    return -1;
}

void build() {
    int ifexist, edge_before;
    char stationName[30];
    FILE *fp = fopen("bgstations.txt", "r");
    fscanf(fp, "%d", &lineNum);
    for (int i = 0; i < lineNum; i++) {
        fscanf(fp, "%d%d", &id, &each_staNum);
        edge_before = -1;
        for (int j = 0; j < each_staNum; j++) {
            fscanf(fp, "%s%d", stationName, &IsChange);
            ifexist = exist(stationName);
            if (ifexist == -1) {
                strcpy(StationID_list[all_staNum].name, stationName);
                StationID_list[all_staNum].change = IsChange;
                if (edge_before != -1)
                    map[edge_before][all_staNum].id = id, map[all_staNum][edge_before].id = id,
                    map[edge_before][all_staNum].wgt = 1, map[all_staNum][edge_before].wgt = 1;

                edge_before = all_staNum;
                all_staNum++;
            } else {
                if (edge_before != -1)
                    map[edge_before][ifexist].id = id, map[ifexist][edge_before].id = id,
                    map[edge_before][ifexist].wgt = 1, map[ifexist][edge_before].wgt = 1;

                edge_before = ifexist;
            }
        }
    }
}

void dijkstra(int src, int dst) {
    int flag[M], min, tmp, ttmp;
    for (int i = 0; i < all_staNum; i++)
        flag[i] = 0, pre[i] = src, dis[i] = map[src][i].wgt;

    dis[src] = 0, flag[src] = 1;
    for (int i = 0; i < all_staNum - 1; i++) {
        min = INF;
        for (int j = 0; j < all_staNum; j++)
            if (flag[j] == 0 && dis[j] < min)
                min = dis[j], ttmp = j;

        flag[ttmp] = 1;
        if (ttmp == dst) return;
        for (int j = 0; j < all_staNum; j++) {
            tmp = (map[ttmp][j].wgt == INF ? INF : (min + map[ttmp][j].wgt));
            if (flag[j] == 0 && (tmp < dis[j]) && map[ttmp][j].id > 0)
                dis[j] = tmp, pre[j] = ttmp;
        }
    }
}

void print(int src, int dst) {
    int u = path[top - 1];
    int i, EdgeLast = u, pathLen = 1;
    int id = map[src][u].id;
    printf("%s-%d", StationID_list[src].name, id);
    for (i = top - 2; i >= 0; i--) {
        u = path[i];
        if (id != map[EdgeLast][u].id) {
            id = map[EdgeLast][u].id;
            printf("(%d)-%s-%d", pathLen, StationID_list[EdgeLast].name, id);
            pathLen = 0;
        }
        pathLen++, EdgeLast = u;
    }
    printf("(%d)-%s", pathLen, StationID_list[dst].name);
}

int main() {
    scanf("%s %s", st, ed);
    for (int i = 0; i < M; i++)
        for (int j = 0; j < M; j++)
            map[i][j].wgt = map[j][i].wgt = INF,
                    map[i][j].id = map[j][i].id = 0;
    build();
    int v1 = exist(st), v2 = exist(ed);
    dijkstra(v1, v2);
    for (int t = v2; t != v1; t = pre[t])
        path[top++] = t;
    print(v1, v2);
    return 0;
}